package cn.juan;

import java.io.*;
import java.util.List;
import java.util.TreeSet;


class School implements Serializable
{
    private String name;
    private String address;
    private int birthYear;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getBirthYear() {
        return birthYear;
    }

    public void setBirthYear(int birthYear) {
        this.birthYear = birthYear;
    }

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", birthYear=" + birthYear +
                '}';
    }

    public School(String name, String address, int birthYear) {
        this.name = name;
        this.address = address;
        this.birthYear = birthYear;
    }
    public void writeToFile(School school1,School school2) throws IOException {
        ObjectOutputStream obs=new ObjectOutputStream(new FileOutputStream("day5/1.dat"));
        obs.writeObject(school1);
        obs.writeObject(school2);
        obs.close();
        System.out.println("写入成功");
    }

    public void readFromFile() throws IOException, ClassNotFoundException {
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("day5/1.dat"));
        try {
            while (true){
                Object obj = ois.readObject();
                System.out.println(obj.toString());
            }
        } catch (EOFException e) {
            System.out.println("文件读取结束");
        }
        ois.close();
    }

}

class  student implements Comparable<student>{
    private int sid;
    private String name;
    private   int age;

    @Override
    public String toString() {
        return "student{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public student(int sid, String name, int age) {
        this.sid = sid;
        this.name = name;
        this.age = age;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int compareTo(student o) {
        if(this.sid<o.sid){
            return -1;
        }else if(this.sid==o.sid){
            return this.age-o.age;
        }
        return 1;
    }
}
public class day5homework {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        School school1=new School("泉大","泉州晋江",2022);
        School school2=new School("泉信","泉州鲤城",2021);
        school1.writeToFile(school1,school2);
        school1.readFromFile();

        TreeSet<student> set1=new TreeSet<>();
        set1.add(new student(1,"张三",19));
        set1.add(new student(2,"李四",20));
        set1.add(new student(3,"王五",20));
        set1.stream().forEach(System.out::println);
        System.out.println("---------------------");
        TreeSet<student> set2=new TreeSet<>();
        set2.add(new student(1,"张三",21));
        set2.add(new student(2,"李四",21));
        set2.add(new student(1,"王五",20));
        set2.stream().forEach(System.out::println);
        System.out.println("----------------------");
        set2.stream().sorted((o1,o2)->o1.getAge()-o2.getAge()).forEach(System.out::println);
    }
}
